package com.auto.survey.activity.adjustment.adjustment_high

import android.graphics.Color
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import com.auto.survey.R
import com.auto.survey.activity.adjustment.adjustment_high.dao.AdjustmentRepository
import com.auto.survey.activity.adjustment.adjustment_high.entity.Project
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CentreToast
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 新建项目
 */
class HighAddActivity : BaseActivity() {

    private val project = Project(name="", discrib = "", personName = "")
    private var project_id = -1
    private val repository = AdjustmentRepository()

    companion object {
        val KEY_ADD_OR_UPDATE_PROJECT_ID = "addOrUpdate"
    }

    override fun initView() {

        project_id = intent.getIntExtra(KEY_ADD_OR_UPDATE_PROJECT_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.blue)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.blue)
        mComment_title?.setTitle("新建项目")

        mComment_title?.setRightTitle("保存")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{
            if (TextUtils.isEmpty(project.name)){
                CentreToast.instance.show("请输入项目名称", false)
                return@debounce
            }

            if (project_id == -1) {
                saveProject()
            } else {
                updateProject()
            }

        })

        val editProjName = findViewById<EditText>(R.id.edit_proj_name)
        val editPorjUserName = findViewById<EditText>(R.id.edit_proj_person)
        val editProjDiscrib = findViewById<EditText>(R.id.edit_proj_discrib)

        editProjName.addTextChangedListener(MyTextWatcher(editProjName))
        editPorjUserName.addTextChangedListener(MyTextWatcher(editPorjUserName))
        editProjDiscrib.addTextChangedListener(MyTextWatcher(editProjDiscrib))

        if (project_id != -1) {
            CoroutineScope(Dispatchers.IO).launch {
                val bean = repository.getProjectById(project_id)
                if (bean != null) {
                    withContext(Dispatchers.Main){
                        project.id = project_id
                        project.name = bean.project.name
                        project.personName = bean.project.personName
                        project.discrib = bean.project.discrib
                        project.createTime = bean.project.createTime
                        project.pointCount = bean.project.pointCount
                        project.observationCount = bean.project.observationCount
                        project.status = bean.project.status

                        editProjName.setText(project.name)
                        editPorjUserName.setText(project.personName)
                        editProjDiscrib.setText(project.discrib)
                    }
                }
            }
        }
    }

    private fun saveProject() {
        CoroutineScope(Dispatchers.IO).launch {
            val succ = repository.addProject(project)
            withContext(Dispatchers.Main) {
                if (succ.toInt() != -1) {
                    CentreToast.instance.show("保存成功", true)
                    finish()
                } else {
                    CentreToast.instance.show("保存失败", false)
                }
            }
        }
    }

    private fun updateProject() {
        CoroutineScope(Dispatchers.IO).launch {
            val succ = repository.updateProject(project)
            withContext(Dispatchers.Main) {
                if (succ != -1) {
                    CentreToast.instance.show("保存成功", true)
                    finish()
                } else {
                    CentreToast.instance.show("保存失败", false)
                }
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_addhigh_proj
    }

    inner class MyTextWatcher(val editText: EditText) : TextWatcher {

        override fun afterTextChanged(s: Editable?) {
            when(editText.id){
                R.id.edit_proj_name-> project.name = s?.toString()?:""
                R.id.edit_proj_person->project.personName = s?.toString()?:""
                R.id.edit_proj_discrib ->project.discrib = s?.toString()?:""
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }


    }
}